package com.peng.sell.controller;

import com.peng.sell.converter.OrderForm2OrderDTOConverter;
import com.peng.sell.dto.OrderDTO;
import com.peng.sell.enums.ResponseCodeEnum;
import com.peng.sell.exception.SellException;
import com.peng.sell.form.OrderForm;
import com.peng.sell.service.BuyerService;
import com.peng.sell.service.OrderService;
import com.peng.sell.vo.ServerResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     * @param orderForm
     * @param bindingResult
     * @return
     */
    @PostMapping("/create")
    public ServerResponseVO<Map<String,String>> create(@Validated OrderForm orderForm, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.error("[创建订单]表单参数错误：orderForm={}",orderForm);
            throw new SellException(ResponseCodeEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.converter(orderForm);
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建订单]购物车不能为空：cart={}",orderDTO.getOrderDetailList());
            throw new SellException(ResponseCodeEnum.CART_EMPTY);
        }
        OrderDTO result = orderService.create(orderDTO);
        Map<String ,String > map = new HashMap<>();
        map.put("orderId",result.getOrderId());
        return ServerResponseVO.createBySuccess(map);
    }


    /**
     * 订单列表
     * @param openid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ServerResponseVO<List<OrderDTO>> list(String openid, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){
        if(StringUtils.isEmpty(openid)){
            log.error("[显示订单列表]openid不能为空：openid={}",openid);
            throw new SellException(ResponseCodeEnum.PARAM_ERROR);
        }
        PageRequest pageRequest = new PageRequest(pageNum,pageSize);
        Page<OrderDTO> result = orderService.findList(openid, pageRequest);
        return ServerResponseVO.createBySuccess("查询成功",result.getContent());
    }

    /**
     * 订单详情
     * @param openid
     * @param orderId
     * @return
     */
    @GetMapping("/detail")
    public ServerResponseVO<OrderDTO> detail(String openid,String orderId){
        if(StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)){
            log.error("[订单详情]参数错误，openid={}，orderId={}",orderId,orderId);
            throw new SellException(ResponseCodeEnum.PARAM_ERROR);
        }
        OrderDTO orderDTO = buyerService.findOneByOrderId(openid,orderId);
        return ServerResponseVO.createBySuccess("查询成功",orderDTO);
    }

    /**
     * 订单取消
     * @param openid
     * @param orderId
     * @return
     */
    @PostMapping("/cancel")
    public ServerResponseVO cancel(String openid,String orderId){
        if(StringUtils.isEmpty(openid) || StringUtils.isEmpty(orderId)){
            log.error("[订单取消]参数错误，openid={}，orderId={}",orderId,orderId);
            throw new SellException(ResponseCodeEnum.PARAM_ERROR);
        }
        buyerService.cancelByOrderId(openid,orderId);
        return ServerResponseVO.createBySuccessMsg("成功");

    }
}
